﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Interfleet.SE.UICompiler.Core.ClosureCompilerREST;
using Interfleet.SE.UICompiler.Core;
using Moq;

namespace Interfleet.SE.UICompiler.Tests.ClosureCompilerREST
{
    /// <summary>
    /// Summary description for RESTResultBuilderTests
    /// </summary>
    [TestClass]
    public class RESTResultBuilderTests
    {
        public RESTResultBuilderTests()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        private string ResponseXml
        {
            get
            {
                return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<compilationResult><compiledCode>function scrollX(){var a=document.documentElement;return self.pageXOffset||a&amp;&amp;a.scrollLeft||document.body.scrollLeft}function scrollY(){var a=document.documentElement;return self.pageYOffset||a&amp;&amp;a.scrollTop||document.body.scrollTop}function windowHeight(){var a=document.documentElement;return self.innerHeight||a&amp;&amp;a.clientHeight||document.body.clientHeight}function windowWidth(){var a=document.documentElement;return self.innerWidth||a&amp;&amp;a.clientWidth||document.body.clientWidth}\nfunction getHeight(a){return parseInt(getStyle(a,\"height\"))}function getWidth(a){return parseInt(getStyle(a,\"width\"))}function setX(a,b){a.style.left=b+\"px\"}function setY(a,b){a.style.top=b+\"px\"}\nfunction getStyle(a,b){if(a.style[b])return a.style[b];else if(a.currentStyle)return a.currentStyle[b];else if(document.defaultView&amp;&amp;document.defaultView.getComputedStyle){b=b.replace(/([A-Z])/g,\"-$1\");b=b.toLowerCase();return(a=document.defaultView.getComputedStyle(a,\"\"))&amp;&amp;a.getPropertyValue(b)}else return null}function setOpacity(a,b){if(a.filters)a.style.filter=\"alpha(opacity=\"+b+\")\";else a.style.opacity=b/100}\nfunction hide(a){var b=getStyle(a,\"display\");if(b!=\"none\")a.$oldDisplay=b;a.style.display=\"none\"}function show(a){a.style.display=a.$oldDisplay||\"block\"}function pageHeight(){return document.body.scrollHeight}function pageWidth(){return document.body.scrollWidth}function fadeIn(a,b,c){setOpacity(a,0);show(a);for(var d=0;d&lt;=100;d+=5)(function(){var e=d;setTimeout(function(){setOpacity(a,e/100*b)},(d+1)*c)})()}\nfunction fadeOut(a,b,c){for(var d=0;d&lt;100;d+=5)(function(){var e=d;setTimeout(function(){setOpacity(a,100-e);e==95&amp;&amp;hide(a)},(d+1)*c)})()}function adjustCenter(a){if(a){var b=getWidth(a),c=getHeight(a);c=scrollY()+windowHeight()/2-c/2;if(c&lt;0)c=0;b=scrollX()+windowWidth()/2-b/2;if(b&lt;0)b=0;setY(a,c);setX(a,b)}}function id(a){return document.getElementById(a)}function tag(a,b){return(b||document).getElementsByTagName(a)}\nfunction byClass(a,b){var c=[];a=new RegExp(\"(^|\\\\s)\"+a+\"(\\\\s|$)\");b=document.getElementsByTagName(b||\"*\");for(var d=0;d&lt;b.length;d++)a.test(b[d].className)&amp;&amp;c.push(b[d]);return c}function next(a){do a=a.nextSibling;while(a&amp;&amp;a.nodeType!=1);return a}function prev(a){do a=a.previousSibling;while(a&amp;&amp;a.nodeType!=1);return a}function addClassName(a,b){var c=a.className;if(!(new RegExp(b,\"i\")).test(c))a.className=c+(c.length&gt;0?\" \":\"\")+b}\nfunction removeClassName(a,b){a.className=a.className.replace(new RegExp(b+\"s?\",\"i\"),\"\").replace(/^\\s?|\\s?$/g,\"\")}function replaceClassName(a,b,c){removeClassName(a,b);addClassName(a,c)}function getElementsByClassName(a,b,c){a=b==\"*\"&amp;&amp;a.all?a.all:a.getElementsByTagName(b);b=[];c=c.replace(/\\-/g,\"\\\\-\");c=new RegExp(\"(^|\\\\s)\"+c+\"(\\\\s|$)\");for(var d,e=0;e&lt;a.length;e++){d=a[e];c.test(d.className)&amp;&amp;b.push(d)}return b}\nfunction getElementsByTags(a){for(var b=[],c,d=0;d&lt;a.length;d++){c=document.getElementsByTagName(a[d]);for(var e=0;e&lt;c.length;e++)b.push(c[e])}return b}function reverseCssFind(a,b){b=new RegExp(\"(^|\\\\s)\"+b+\"(\\\\s|$)\");for(a=a.parentNode;a;){if(b.test(a.className))break;a=a.parentNode}return a}\nfunction getElementPosition(a){if(a=$get(a)){for(var b=0,c=0;a;){b+=a.offsetLeft;c+=a.offsetTop;a=a.offsetParent}if(navigator.userAgent.indexOf(\"Mac\")!=-1&amp;&amp;typeof document.body.leftMargin!=\"undefined\"){b+=document.body.leftMargin;c+=document.body.topMargin}return{left:b,top:c}}};</compiledCode></compilationResult>";
            }
        }

        private string EmptyResponseXml
        {
            get
            {
                return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<compilationResult><compiledCode></compiledCode></compilationResult>";
            }
        }

        [TestMethod]
        public void RESTResultBuilder_Build_When_ParametersAreValid_Then_ResultStatusIsSuccess()
        {
            var fileProvider = new Mock<IFileProvider>();
            fileProvider.Setup(p => p.SaveToFile(It.IsAny<string>(), It.IsAny<string>()));
            var argument = new CCRESTArguments();
            argument.Files.Add("a");
            argument.CompiledFileName = "b";
            var builder = new CCRESTResultBuilder(argument, fileProvider.Object);
            var result = builder.Build(ResponseXml, DateTime.Now);
            Assert.IsTrue(result.Success);
        }

        [TestMethod]
        public void RESTResultBuilder_Build_When_ParametersAreInValid_Then_ResultStatusIsNotSuccess()
        {
            var fileProvider = new Mock<IFileProvider>();
            fileProvider.Setup(p => p.SaveToFile(It.IsAny<string>(), It.IsAny<string>()));
            var argument = new CCRESTArguments();
            argument.Files.Add("a");
            argument.CompiledFileName = "b";
            argument.CompileOption = "WHITESPACE_ONLY";
            var builder = new CCRESTResultBuilder(argument, fileProvider.Object);
            var result = builder.Build(EmptyResponseXml, DateTime.Now);
            Assert.IsFalse(result.Success);
        }
    }
}
